package com.daluzy.chmanager.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.daluzy.chmanager.R;
import com.daluzy.chmanager.a_interface.RecyclerViewItemOnClickListener;
import com.daluzy.chmanager.adapter.SelectSchoolAdapter;
import com.daluzy.chmanager.bean.School;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.xiasuhuei321.loadingdialog.view.LoadingDialog;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;

public class SelectSchoolActivity extends AppCompatActivity {
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.rv_data)
    RecyclerView rvData;
    @BindView(R.id.tv_confirm)
    TextView tvConfirm;
    private SelectSchoolAdapter selectSchoolAdapter;
    private Context context;
    private LoadingDialog loadingDialog;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_select_school_daluzy);
        ButterKnife.bind(this);
        loadingDialog = new LoadingDialog(this);
        context = this;
        rvData.setLayoutManager(new LinearLayoutManager(this));
        initData();
    }

    private void initData() {
        if (loadingDialog != null) {
            loadingDialog.show();
        }
        BmobQuery<School> query = new BmobQuery<>();
        query.findObjects(new FindListener<School>() {
            @Override
            public void done(List<School> object, BmobException e) {
                if (e == null) {
                    if (object != null && object.size() > 0) {
                        selectSchoolAdapter = new SelectSchoolAdapter(context, object);
                        selectSchoolAdapter.setItemOnClickListener(new RecyclerViewItemOnClickListener<School>() {
                            @Override
                            public void itemOnClick(School school, int position) {
                                selectSchoolAdapter.updateItem(position);
                            }
                        });
                        rvData.setAdapter(selectSchoolAdapter);
                    }
                } else {
                    Toast.makeText(context, e.getMessage(), Toast.LENGTH_SHORT).show();
                }
                if (loadingDialog != null) {
                    loadingDialog.close();
                }
            }
        });
    }

    @OnClick({R.id.iv_back, R.id.tv_confirm})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.tv_confirm:
                School schoolSelect = null;
                if (selectSchoolAdapter != null)
                    schoolSelect = selectSchoolAdapter.getSelectedSchool();
                Intent intent = getIntent();
                if (schoolSelect != null)
                    intent.putExtra("school", schoolSelect);
                setResult(2021, intent);
                finish();
                break;
        }
    }
}
